package com.mobinters.parser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {
	int timeOut=3000;
	/*
	 * Constructor
	 */
	public JSONParser(){
		
	}
	/*
	 * Get String Content from URL GET, not care about JSON or XML or TEXT
	 */
	public String getStringFromGET(String url){
		String content = null;

		try {
			// defaultHttpClient
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet=new HttpGet(url);
			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity httpEntity = httpResponse.getEntity();
			content = EntityUtils.toString(httpEntity);
			

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// return XML
		return content;
	}
	
	
	/*
	 * Get JSON Object from URL POST
	 */
	public JSONObject getJSONFromGET(String url){
		JSONObject jObj=null;
		try{
			// defaultHttpClient
						DefaultHttpClient httpClient = new DefaultHttpClient();
						HttpGet httpGet=new HttpGet(url);

						HttpResponse httpResponse = httpClient.execute(httpGet);
						HttpEntity httpEntity = httpResponse.getEntity();
						InputStream is = httpEntity.getContent();
						BufferedReader reader = new BufferedReader(new InputStreamReader(
			                    is, "iso-8859-1"), 8);
			            StringBuilder sb = new StringBuilder();
			            String line = null;
			            while ((line = reader.readLine()) != null) {
			                sb.append(line + "n");
			            }
			            is.close();
			            Log.e("test-getJsonfromGet",sb.toString());
			            jObj = new JSONObject(sb.toString());
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
		
		return jObj;
	}
		
	/*
	 * Get JSON Object from URL + Params POST
	 */
	public JSONObject getJSONFromPOST(String baseURL, List<NameValuePair> params){
		JSONObject jObj=null;
		try{
			// defaultHttpClient
						DefaultHttpClient httpClient = new DefaultHttpClient();
						HttpPost httpPost = new HttpPost(baseURL);
						/*
						 * EMBED PARAMS --> POST
						 */
						httpPost.setEntity(new UrlEncodedFormEntity(params));

						HttpResponse httpResponse = httpClient.execute(httpPost);
						HttpEntity httpEntity = httpResponse.getEntity();
						InputStream is = httpEntity.getContent();
						BufferedReader reader = new BufferedReader(new InputStreamReader(
			                    is, "iso-8859-1"), 8);
			            StringBuilder sb = new StringBuilder();
			            String line = null;
			            while ((line = reader.readLine()) != null) {
			                sb.append(line + "n");
			            }
			            is.close();
			            
			            jObj = new JSONObject(sb.toString());
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
		
		return jObj;
	}
	

}
